import { IFeidaoAiNodejsComponent } from '@feidao/web/interfaces';
import get_menus from '@feidao/web/atom/nodejs/authorization/get-menus-1';
import render from '@feidao/web/atom/nodejs/render/render';

function render_data(data: any[], is_open: boolean) {

	const t = (p: any) => {
		return data.filter((d: any) => {
			return d.parent_no === p;
		});
	};
	const d1 = data.filter((d: any) => {
		d.c = t(d.menu_no);
		return d.parent_no === '';
	});

	let r = ``;
	let r2 = ``;
	const t1 = (datas: any) => {
		data.forEach((d: any) => {
			if (d.c.length === 0) {
				let str = ``;
				if (d.package_url) {
					str = `<li class="text-l"><a class="font12 box" style="padding-left:15px" href="` + d.package_url + `"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span>` + d.menu_name + `</span></a></li>`;
				} else {
					str = `<li><a style="cursor:default;display:flex;justify-content:  left;margin-left:15px;align-items:  center;"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span>` + d.menu_name + `</span></a></li>`;
				}
				r += str;
				r2 += str;
			} else {
				let str = ``;
				if (d.package_url) {
					str = `<li><a href="` + d.package_url + `"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span>` + d.menu_name + `</span></a><ul>`;
				} else {
					str = `<li><a style="cursor:default;display:flex;justify-content:  left;margin-left:15px;align-items:  center;"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span>` + d.menu_name + `</span></a><ul>`;
				}
				r += str;
				r2 += str;
				t1(d.c);
			}
		});
		r += `</ul></li>`;
		r2 += `</ul></li>`;
	};
	d1.forEach((d: any) => {
		if (d.c.length === 0) {
			if (d.package_url) {
				r += `<li><a href="` + d.package_url + `"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span>` + d.menu_name + `</span></a></li>`;
				r2 += `<li><a href="` + d.package_url + `"><i class="iconfont font22 display-ib ` + d.icon + `"></i></a></li>`;
			} else {
				r += `<li><a style="cursor:default;display:flex;justify-content:  left;margin-left:15px;align-items:  center;"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span style="margin-left:5px;>` + d.menu_name + `</span></a></li>`;
				r2 += `<li><a style="cursor:default;display:flex;justify-content:  left;margin-left:15px;align-items:  center;"><i class="iconfont font22 display-ib ` + d.icon + `"></i></a></li>`;
			}
		} else {
			if (d.package_url) {
				r += `<li><a href="` + d.package_url + `"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span>` + d.menu_name + `</span></a><ul>`;
				r2 += `<li><a href="` + d.package_url + `"><i class="iconfont font22 display-ib ` + d.icon + `"></i></a><ul>`;
			} else {
				r += `<li><a style="cursor:default;display:flex;justify-content:  left;margin-left:15px;align-items:  center;"><i class="iconfont font22 display-ib ` + d.icon + `"></i><span style="margin-left:5px;">` + d.menu_name + `</span></a><ul>`;
				r2 += `<li><a style="cursor:default;display:flex;justify-content:  left;margin-left:15px;align-items:  center;"><i class="iconfont font22 display-ib ` + d.icon + `"></i></a><ul>`;
			}
			t1(d.c);
		}
	});
	r += `</ul></li>`;
	r2 += `</ul></li>`;
	if (is_open) {
		return r;
	} else {
		return r2;
	}
}
export default async function na001(fd: IFeidaoAiNodejsComponent, ...args: any[]) {
	const menus = await get_menus(fd.data.msg, fd.data.headers, 'feidao_mes', '3') as any;
	render(fd.data.node, '', render_data(menus, true), 'P01');
	render(fd.data.node, '', render_data(menus, false), 'P02');
}
